<?php
namespace app\index\controller;

use think\Request;
use \app\admin\model\Product as ProductModel;
use \app\admin\model\Category;

class Product extends Base
{
    protected $product_model;
    protected $cate_model;
    
    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->product_model = new ProductModel();

        $this->cate_model = new Category();
    }

    public function prolist($id = null)
    {
        if ($id == null) {
            return $this->redirect('index/index');
        } else {
            $prolist = $this->product_model->where('cid', 'in', $id)
                    ->where('isshow', 1)
                    ->order('istop')
                    ->order('id', 'desc')
                    ->paginate(4);
    
            $this->assign('data', $prolist);
    
            return $this->fetch('prolist');
        }
    }

    public function build($id = 51)
    {
        $ids = $this->cate_model->getIds($id);

        return $this->prolist($ids);
    }

    public function probuild($id = null)
    {
        if ($id == null) {
            return $this->redirect('index/index');
        } else {
            $content = $this->product_model->find($id);

            $content->hits +=1;
            
            @$content->save();
    
            $this->assign('data', $content);
    
            return $this->fetch('probuild');
        }
    }

    public function prolists($id = null)
    {
        if ($id == null) {
            return $this->redirect('index/index');
        } else {
            $prolists = $this->product_model->where('cid', 'in', $id)
                    ->where('isshow', 1)
                    ->order('istop')
                    ->order('id', 'desc')
                    ->paginate(6);
    
            $this->assign('data', $prolists);
    
            return $this->fetch('prolists');
        }
    }

    public function design($id = 61)
    {
        $ids = $this->cate_model->getIds($id);

        return $this->prolists($ids);
    }

    public function prodesign($id = 61)
    {
        if ($id == null) {
            return $this->redirect('index/index');
        } else {
            $content = $this->product_model->find($id);
    
            $this->assign('data', $content);

            $ids = $this->cate_model->getIds(61);
            $prolists = $this->product_model->where('cid', 'in', $ids)
                ->where('isshow', 1)
                ->where('id', 'notin', $id)
                ->limit(3)
                ->order('istop')
                ->order('id', 'desc')
                ->select();
    
            $this->assign('prolists', $prolists);
    
            return $this->fetch('prodesign');
        }
    }
}
